﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Slja.SMS.DAL
{
    /// <summary>
    /// 数据访问类:sms_MsgSend
    /// </summary>
    internal partial class sms_MsgSend
    {
        public sms_MsgSend()
        { }
        #region  Method

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Slja.SMS.Model.sms_MsgSend model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into dbo.sms_MsgSend(");
            strSql.Append("SendStatus,SendUser,SendTime,RecvNO,MsgContent)");
            strSql.Append(" values (");
            strSql.Append("@SendStatus,@SendUser,@SendTime,@RecvNO,@MsgContent)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@SendStatus", SqlDbType.TinyInt,1),
					new SqlParameter("@SendUser", SqlDbType.Int,4),
					new SqlParameter("@SendTime", SqlDbType.DateTime),
					new SqlParameter("@RecvNO", SqlDbType.NVarChar,20),
					new SqlParameter("@MsgContent", SqlDbType.NVarChar,100)};
            parameters[0].Value = model.SendStatus;
            parameters[1].Value = model.SendUser;
            parameters[2].Value = model.SendTime;
            parameters[3].Value = model.RecvNO;
            parameters[4].Value = model.MsgContent;

            object obj = Slja.DBUtility.SqlServerHelper.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 增加多条数据
        /// </summary>
        public void Add(List<Slja.SMS.Model.sms_MsgSend> modellist)
        {
            StringBuilder strSql = new StringBuilder();
            SqlParameter[] parameters = new SqlParameter[modellist.Count*5];
            for (int i = 0; i < modellist.Count; i++)
            {
                Slja.SMS.Model.sms_MsgSend model = modellist[i];
                strSql.Append("insert into dbo.sms_MsgSend(");
                strSql.Append("SendStatus,SendUser,SendTime,RecvNO,MsgContent)");
                strSql.Append(" values (");
                strSql.AppendFormat("@SendStatus{0},@SendUser{0},@SendTime{0},@RecvNO{0},@MsgContent{0});", i);

                (parameters[0 + i * 5] = new SqlParameter("@SendStatus" + i.ToString(), SqlDbType.TinyInt, 1)).Value = model.SendStatus;
                (parameters[1 + i * 5] = new SqlParameter("@SendUser" + i.ToString(), SqlDbType.Int, 4)).Value = model.SendUser;
                (parameters[2 + i * 5] = new SqlParameter("@SendTime" + i.ToString(), SqlDbType.DateTime)).Value = model.SendTime;
                (parameters[3 + i * 5] = new SqlParameter("@RecvNO" + i.ToString(), SqlDbType.NVarChar, 20)).Value = model.RecvNO;
                (parameters[4 + i * 5] = new SqlParameter("@MsgContent" + i.ToString(), SqlDbType.NVarChar, 100)).Value = model.MsgContent;
            }
           Slja.DBUtility.SqlServerHelper.ExecuteSql(strSql.ToString(), parameters);
           
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Slja.SMS.Model.sms_MsgSend GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,SendStatus,SendUser,SendTime,RecvNO,MsgContent from sms_MsgSend ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
            parameters[0].Value = ID;

            Slja.SMS.Model.sms_MsgSend model = new Slja.SMS.Model.sms_MsgSend();
            DataSet ds = Slja.DBUtility.SqlServerHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["SendStatus"] != null && ds.Tables[0].Rows[0]["SendStatus"].ToString() != "")
                {
                    model.SendStatus = uint.Parse(ds.Tables[0].Rows[0]["SendStatus"].ToString());
                }
                if (ds.Tables[0].Rows[0]["SendUser"] != null && ds.Tables[0].Rows[0]["SendUser"].ToString() != "")
                {
                    model.SendUser = int.Parse(ds.Tables[0].Rows[0]["SendUser"].ToString());
                }
                if (ds.Tables[0].Rows[0]["SendTime"] != null && ds.Tables[0].Rows[0]["SendTime"].ToString() != "")
                {
                    model.SendTime = DateTime.Parse(ds.Tables[0].Rows[0]["SendTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RecvNO"] != null && ds.Tables[0].Rows[0]["RecvNO"].ToString() != "")
                {
                    model.RecvNO = ds.Tables[0].Rows[0]["RecvNO"].ToString();
                }
                if (ds.Tables[0].Rows[0]["MsgContent"] != null && ds.Tables[0].Rows[0]["MsgContent"].ToString() != "")
                {
                    model.MsgContent = ds.Tables[0].Rows[0]["MsgContent"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        #endregion  Method
    }
    /// <summary>
    /// 数据访问类:sms_MsgRecive
    /// </summary>
    internal partial class sms_MsgRecive
    {
        public sms_MsgRecive()
        { }
        #region  Method
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Slja.SMS.Model.sms_MsgRecive model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into sms_MsgRecive(");
            strSql.Append("Status,SendNO,RecvTime,MsgIndex,MsgContent,Code,ReadMemo)");
            strSql.Append(" values (");
            strSql.Append("@Status,@SendNO,@RecvTime,@MsgIndex,@MsgContent,@Code,@ReadMemo)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Status", SqlDbType.TinyInt,1),
					new SqlParameter("@SendNO", SqlDbType.NVarChar,20),
					new SqlParameter("@RecvTime", SqlDbType.DateTime),
					new SqlParameter("@MsgIndex", SqlDbType.Int,4),
					new SqlParameter("@MsgContent", SqlDbType.NVarChar,1000),
					new SqlParameter("@Code", SqlDbType.NVarChar,20),
					new SqlParameter("@ReadMemo", SqlDbType.NVarChar,2000)};
            parameters[0].Value = model.Status;
            parameters[1].Value = model.SendNO;
            parameters[2].Value = model.RecvTime;
            parameters[3].Value = model.MsgIndex;
            parameters[4].Value = model.MsgContent;
            parameters[5].Value = model.Code;
            parameters[6].Value = model.ReadMemo;

            object obj = Slja.DBUtility.SqlServerHelper.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 增加多条数据
        /// </summary>
        public List<int> Add(List<Slja.SMS.Model.sms_MsgRecive> modellist)
        {
            StringBuilder strSql = new StringBuilder("declare @t table(id int);");
            SqlParameter[] parameters = new SqlParameter[modellist.Count * 7];
            for (int i = 0; i < modellist.Count; i++)
            {
                Slja.SMS.Model.sms_MsgRecive model = modellist[i];              
                strSql.Append("insert into sms_MsgRecive(");
                strSql.Append("Status,SendNO,RecvTime,MsgIndex,MsgContent,Code,ReadMemo)");
                strSql.Append(" values (");
                strSql.AppendFormat("@Status{0},@SendNO{0},@RecvTime{0},@MsgIndex{0},@MsgContent{0},@Code{0},@ReadMemo{0});",i.ToString());
                strSql.Append("insert into @t(id) select @@identity;");             
              
                (parameters[0 + i * 7] = new SqlParameter("@Status"+i.ToString(), SqlDbType.TinyInt, 1)).Value = model.Status;
                (parameters[1 + i * 7] = new SqlParameter("@SendNO" + i.ToString(), SqlDbType.NVarChar, 20)).Value = model.SendNO;
                (parameters[2 + i * 7] = new SqlParameter("@RecvTime" + i.ToString(), SqlDbType.DateTime)).Value = model.RecvTime;
                (parameters[3 + i * 7] = new SqlParameter("@MsgIndex" + i.ToString(), SqlDbType.Int, 4)).Value = model.MsgIndex;
                (parameters[4 + i * 7] = new SqlParameter("@MsgContent" + i.ToString(), SqlDbType.NVarChar, 1000)).Value = model.MsgContent;
                (parameters[5 + i * 7] = new SqlParameter("@Code" + i.ToString(), SqlDbType.NVarChar, 20)).Value = model.Code;
                (parameters[6 + i * 7] = new SqlParameter("@ReadMemo" + i.ToString(), SqlDbType.NVarChar, 2000)).Value = model.ReadMemo;
            }
            strSql.Append(" select [id] from @t;");
            DataSet ds = Slja.DBUtility.SqlServerHelper.Query(strSql.ToString(), parameters);
            List<int> ListID = new List<int>();
            for (int k = 0; k < ds.Tables[0].Rows.Count; k++)
            {
                ListID.Add((int)ds.Tables[0].Rows[k]["id"]);
            }
            return ListID;
        }
        public int Read(int ID,string UserName)
        {
            string sql = "update dbo.sms_MsgRecive set Status=1,ReadMemo=ReadMemo+'|'+@UserName+'*'+cast(getdate() as varchar(20)) where ID=@ID";
            SqlParameter[] parms = new SqlParameter[2];
            (parms[0]=new SqlParameter("@ID",SqlDbType.Int,4)).Value = ID;
            (parms[1]=new SqlParameter("@UserName",SqlDbType.NVarChar,20)).Value = UserName;
            return Slja.DBUtility.SqlServerHelper.ExecuteSql(sql, parms);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Slja.SMS.Model.sms_MsgRecive model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update sms_MsgRecive set ");
            strSql.Append("Status=@Status,");
            strSql.Append("SendNO=@SendNO,");
            strSql.Append("RecvTime=@RecvTime,");
            strSql.Append("MsgIndex=@MsgIndex,");
            strSql.Append("MsgContent=@MsgContent,");
            strSql.Append("Code=@Code,");
            strSql.Append("ReadMemo=@ReadMemo");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@Status", SqlDbType.TinyInt,1),
					new SqlParameter("@SendNO", SqlDbType.NVarChar,20),
					new SqlParameter("@RecvTime", SqlDbType.DateTime),
					new SqlParameter("@MsgIndex", SqlDbType.Int,4),
					new SqlParameter("@MsgContent", SqlDbType.NVarChar,100),
					new SqlParameter("@Code", SqlDbType.NVarChar,20),
					new SqlParameter("@ReadMemo", SqlDbType.NVarChar,2000),
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = model.Status;
            parameters[1].Value = model.SendNO;
            parameters[2].Value = model.RecvTime;
            parameters[3].Value = model.MsgIndex;
            parameters[4].Value = model.MsgContent;
            parameters[5].Value = model.Code;
            parameters[6].Value = model.ReadMemo;
            parameters[7].Value = model.ID;

            int rows = Slja.DBUtility.SqlServerHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Slja.SMS.Model.sms_MsgRecive GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,Status,SendNO,RecvTime,MsgIndex,MsgContent,Code,ReadMemo from sms_MsgRecive ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
            parameters[0].Value = ID;

            Slja.SMS.Model.sms_MsgRecive model = new Slja.SMS.Model.sms_MsgRecive();
            DataSet ds = Slja.DBUtility.SqlServerHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Status"] != null && ds.Tables[0].Rows[0]["Status"].ToString() != "")
                {
                    model.Status = int.Parse(ds.Tables[0].Rows[0]["Status"].ToString());
                }
                if (ds.Tables[0].Rows[0]["SendNO"] != null && ds.Tables[0].Rows[0]["SendNO"].ToString() != "")
                {
                    model.SendNO = ds.Tables[0].Rows[0]["SendNO"].ToString();
                }
                if (ds.Tables[0].Rows[0]["RecvTime"] != null && ds.Tables[0].Rows[0]["RecvTime"].ToString() != "")
                {
                    model.RecvTime = DateTime.Parse(ds.Tables[0].Rows[0]["RecvTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MsgIndex"] != null && ds.Tables[0].Rows[0]["MsgIndex"].ToString() != "")
                {
                    model.MsgIndex = int.Parse(ds.Tables[0].Rows[0]["MsgIndex"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MsgContent"] != null && ds.Tables[0].Rows[0]["MsgContent"].ToString() != "")
                {
                    model.MsgContent = ds.Tables[0].Rows[0]["MsgContent"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Code"] != null && ds.Tables[0].Rows[0]["Code"].ToString() != "")
                {
                    model.Code = ds.Tables[0].Rows[0]["Code"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ReadMemo"] != null && ds.Tables[0].Rows[0]["ReadMemo"].ToString() != "")
                {
                    model.ReadMemo = ds.Tables[0].Rows[0]["ReadMemo"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        #endregion  Method
    }
   
   
}